package com.metaera.app.ui.activities;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;


import com.metaera.app.R;
import com.metaera.app.ui.core.BaseActivity;


public class WebViewActivity extends BaseActivity {

    public static final String EXTRA_URL = "EXTRA_URL";

    public static Intent newIntent(Context context, String url) {
        return new Intent(context, WebViewActivity.class).putExtra(EXTRA_URL, url);
    }

     WebView  mWebView;
     TextView mTitleTextView;
    ProgressBar mProgressBar;
    ImageView   mArrowBackImage;

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_web_view;
    }

    @Override
    protected void initialize(Bundle savedInstanceState) {
        mWebView = findViewById(R.id.web_view);
        mTitleTextView = findViewById(R.id.tv_title);
        mProgressBar = findViewById(R.id.progress_bar);
        mArrowBackImage = findViewById(R.id.iv_arrow_back);

        mArrowBackImage.setOnClickListener(v -> onBackPressed());

        initSetting(mWebView);

        mWebView.loadUrl(getIntent().getStringExtra(WebViewActivity.EXTRA_URL));

        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                if (mTitleTextView != null) {
                    mTitleTextView.setText(title);
                }
            }

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (mProgressBar != null) {
                    mProgressBar.setProgress(newProgress);
                    if (newProgress == 100) {
                        mProgressBar.setVisibility(View.GONE);
                    }
                }
            }
        });

        mWebView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (Build.VERSION.SDK_INT < 26 && mWebView != null) {
                    mWebView.loadUrl(url);
                    return true;
                }

                return false;
            }
        });
    }


    @SuppressLint("SetJavaScriptEnabled")
    private void initSetting(WebView webView) {
        WebSettings webSettings = webView.getSettings();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

        webSettings.setLoadWithOverviewMode(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setDisplayZoomControls(false);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setTextZoom(100);
        webSettings.setAppCacheMaxSize(10 * 1024 * 1024);
        webSettings.setAppCacheEnabled(true);
        webSettings.setAppCachePath(getDir("appCache", 0).getPath());
        webSettings.setAllowFileAccess(true);
        webSettings.setSavePassword(false);
        webSettings.setLoadsImagesAutomatically(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    }

}
